<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <!-- <label class="el-form-item-label">帐户编号</label> -->
        <!-- <el-input v-model.trim="query.accountId" clearable placeholder="帐户编号" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" /> -->
        <label class="el-form-item-label">用户名</label>
        <el-input v-model.trim="query.accountName" clearable placeholder="用户名" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="800px">
        <el-form ref="form" :model="form" :inline="true" :rules="rules" size="small" label-width="110px">
          <el-form-item v-if="false " label="编号" prop="id">
            <el-input v-model.trim="form.id" style="width: 240px;" />
          </el-form-item>
          <!-- <el-form-item label="帐户编号">
            <el-input v-model.trim="form.accountId" style="width: 240px;" />
          </el-form-item> -->
          <el-form-item label="用户名">
            <el-input v-model.trim="form.accountName" style="width: 240px;" />
          </el-form-item>
          <el-form-item label="系统名称">
            <el-select v-model="form.systemId" filterable placeholder="请选择" style="width: 240px;" @change="cityGet(form.cityId)">
              <el-option
                v-for="item in system"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="部门">
            <el-select v-model="form.cityId" filterable placeholder="请选择" style="width: 240px;">
              <el-option
                v-for="item in city"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="岗位名称">
            <el-select v-model="form.jobId" filterable placeholder="请选择" style="width: 240px;">
              <el-option
                v-for="item in job"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="兑换积分额(元)">
            <el-input v-model="form.exchangeIntegral" type="number" style="width: 240px;" />
          </el-form-item>
          <el-form-item label="兑换状态">
            <el-select v-model="form.exchangeStatus" filterable placeholder="请选择" style="width: 240px;">
              <el-option
                v-for="item in dict.integral_exchange_exchange_status"
                :key="item.id"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <!-- <el-form-item label="兑换批次">
            <el-input v-model.trim="form.exchangeCard" style="width: 240px;" />
          </el-form-item> -->
          <el-form-item label="发起人">
            <el-input v-model.trim="form.exchangeName" style="width: 240px;" />
          </el-form-item>
          <el-form-item label="发起人编号">
            <el-input v-model.trim="form.exchangeId" style="width: 240px;" />
          </el-form-item>
          <el-form-item label="兑换时间">
            <el-date-picker v-model="form.exchangeTime" type="datetime" style="width: 240px;" />
          </el-form-item>
          <el-form-item label="备注">
            <el-input v-model.trim="form.remark" style="width: 240px;" />
          </el-form-item>

        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <!-- <el-table-column prop="id" label="编号" /> -->
        <!-- <el-table-column prop="accountId" label="帐户编号" /> -->
        <el-table-column prop="accountName" label="用户名" align="center" />
        <!-- <el-table-column v-if="false" prop="systemId" label="systemId" /> -->
        <el-table-column prop="phone" label="电话号码" />
        <!-- <el-table-column v-if="false" prop="cityId" label="城市id" /> -->
        <el-table-column prop="cityName" label="部门" align="center" />
        <!-- <el-table-column v-if="false" prop="jobId" label="岗位编号" /> -->
        <!-- <el-table-column prop="jobName" label="岗位名称" /> -->
        <el-table-column label="兑换积分额(元)" align="center">
          <template slot-scope="scope">
            {{ (scope.row.exchangeIntegral/100).toFixed(2) }}
          </template>
        </el-table-column>
        <el-table-column prop="exchangeStatus" label="兑换状态" align="center">
          <template slot-scope="scope">
            {{ dict.label.integral_exchange_exchange_status[scope.row.exchangeStatus] }}
          </template>
        </el-table-column>
        <!-- <el-table-column prop="exchangeCard" label="兑换批次" align="center" /> -->
        <el-table-column prop="exchangeName" label="发起人" align="center" />
        <el-table-column v-if="false" prop="exchangeId" label="积分兑换发起人id" align="center" />
        <el-table-column prop="createTime" label="创建时间" align="center">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="exchangeTime" label="兑换时间" align="center">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.exchangeTime) }}</span>
          </template>
        </el-table-column>
        <!-- <el-table-column prop="remark" label="备注" /> -->
        <el-table-column
          label="操作"
          width="200px"
          align="center"
        >
          <template slot-scope="scope">
            <div v-permission="['admin','integralExchange:manager']">
              <el-button
                type="success"
                style="width: 80px;"
                size="mini"
                :disabled="scope.row.exchangeStatus === 2?false:true"
                @click="detail(scope.row, 3)"
              >接单</el-button>
              <el-button
                type="danger"
                size="mini"
                style="width: 80px;"
                :disabled="scope.row.exchangeStatus === 3?false:true"
                @click="detail(scope.row, 4)"
              >处理完毕</el-button>
            </div>
            <el-button
              v-for="item in dict.integral_detail_exchange_status"
              :key="item.id"
              v-permission="['admin','integralExchange:service']"
              style="margin: 5px; width: 80px;"
              type="warning"
              size="mini"
              :disabled="scope.row.exchangeStatus === 4?false:true"
              @click="detail(scope.row, item.value)"
            >{{ item.label }}</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudIntegralExchange from '@/api/integral/integralExchange'
import { confirmExchange } from '@/api/integral/integralExchange'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import pagination from '@crud/Pagination'
import crudDept from '@/api/system/dept'
import crudJob from '@/api/system/job'

const defaultForm = { id: null, accountId: null, accountName: null, cityId: null, jobId: null, jobName: null, exchangeIntegral: null, exchangeStatus: null, exchangeCard: null, exchangeName: null, exchangeId: null, createTime: null, exchangeTime: null, remark: null, systemId: null, systemName: null, phone: null }
export default {
  name: 'IntegralExchange',
  components: { pagination, crudOperation, rrOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ['integral_exchange_exchange_status', 'integral_detail_exchange_status'],
  cruds() {
    return CRUD({ title: '积分兑换', url: 'api/integralExchange', idField: 'id', sort: 'id,desc', crudMethod: { ...crudIntegralExchange },
      optShow: {
        add: false,
        edit: false,
        del: false,
        download: true,
        reset: true
      }})
  },
  data() {
    return {
      permission: {
        manager: ['admin', 'integralExchange:manager'],
        service: ['admin', 'integralExchange:service']
      },
      rules: {
        // id: [
        //   { required: true, message: '编号不能为空', trigger: 'blur' }
        // ]
      },
      queryTypeOptions: [
        { key: 'accountId', display_name: '帐户编号' },
        { key: 'accountName', display_name: '帐户名称' }
      ],
      system: '',
      city: '',
      job: ''
    }
  },
  mounted() {
    this.systemGet()
    this.jobGet()
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh](a, b) {
      return true
    },
    [CRUD.HOOK.afterToCU](crud, form) {
      form.exchangeStatus = form.exchangeStatus.toString()
    },
    systemGet() {
      // 获取系统下拉框数据
      let pro = {}
      const that = this
      setTimeout(() => {
        crudDept.getDepts({ pidIsNull: true }).then(res => {
          pro = res
          that.system = res.content
        })
      }, 100)
    },
    cityGet(id) {
      let pro = {}
      const that = this
      setTimeout(() => {
        crudDept.getDeptsListPidIsNull({ pid: id }).then(res => {
          pro = res
          that.city = res.content
        })
      }, 100)
    },
    jobGet() {
      let pro = {}
      const that = this
      setTimeout(() => {
        crudJob.getAllJob().then(res => {
          pro = res
          that.job = res.content
        })
      }, 100)
    },
    detail(val, index) {
      const params = { id: val.id, exchangeStatus: parseInt(index) }
      confirmExchange(params).then(res => {
        this.$message.success('操作成功')
        this.crud.toQuery()
      })
    }
  }
}
</script>

<style scoped lang="scss">
  .head-container {
    /** 去除input输入框样式 */
    ::v-deep  input[type=number]::-webkit-inner-spin-button {
      -webkit-appearance: none;
    }
    ::v-deep input[type="number"] {
        -moz-appearance:textfield;
    }
  }
</style>
